"use strict";

const { Controller } = require("egg");
class SiteController extends Controller {
  async queryDataSource() {
    const body = this.ctx.request.body;

    const page = 1; // 当前页码
    const limit = 10; // 每页显示的记录数
    const offset = (page - 1) * limit; // 计算偏移量
    const res = await this.app.model.TDataSource.findAndCountAll({
      where: {
        projectId: body.projectId,
      },
      limit,
      offset,
    });
    this.ctx.body = {
      data: res.rows,
      total: res.count,
      success: true,
    };
  }
  async findSiteMenu(){
    const body  = this.ctx.request.body
    const where = {};
    if(body.id){
      where.id = body.id
    }
    if(body.pathname!==undefined){
      where.path = body.pathname
    }
    if(body.siteId){
      where.siteId = body.siteId
    }
    
    console.log('wherewhere',where)
    this.ctx.body = await this.app.model.TMenu.findOne({
      where: where,
    });
  }
  async saveSiteMenu(){
    const body  = this.ctx.request.body
    const data = this.ctx.request.body;
    if (data.id!==undefined) {
      const res = await this.app.model.TMenu.update(data, {
        where: {
          id:data.id
        },
      });
    }else{
      const res = await this.app.model.TMenu.create(data, {
        where: {},
      });
    }
  
    this.ctx.body = {
      code: 200,
    };
  }
  async querySiteMenu(){
    const body  = this.ctx.request.body
   
    const res = await this.service.menu.queryMenu(body.siteId);
    this.ctx.body = res;
  }
  async saveSite() {
    const data = this.ctx.request.body;
    if (data.id!==undefined) {
      const res = await this.app.model.TSite.update(data, {
        where: {
          id:data.id
        },
      });
    }else{
      const res = await this.app.model.TSite.create(data, {
        where: {},
      });
    }
  
    this.ctx.body = {
      code: 200,
    };
  }
  async queryConfig(){
    const data = this.ctx.request.body;

    const menus = await this.service.menu.queryMenu(data.siteId)
    const config = await this.app.model.TSite.findOne({where:{id:data.siteId}})
    this.ctx.body ={
      menus,
      config
    }
  }
  async querySiteList() {
    const page = 1; // 当前页码
    const limit = 10; // 每页显示的记录数
    const offset = (page - 1) * limit; // 计算偏移量
    const res = await this.app.model.TSite.findAndCountAll({
      where: {},
      limit,
      offset,
    });
    this.ctx.body = {
      data: res.rows,
      total: res.count,
      success: true,
    };
  }
  async queryProjectList() {
    const page = 1; // 当前页码
    const limit = 10; // 每页显示的记录数
    const offset = (page - 1) * limit; // 计算偏移量
    const res = await this.app.model.TProject.findAndCountAll({
      where: {},
      limit,
      offset,
    });
    this.ctx.body = {
      data: res.rows,
      total: res.count,
      success: true,
    };
  }
  async saveProject() {
    const data = this.ctx.request.body;
    if (data.id) {
      const res = await this.app.model.TProject.update(data, {
        where: {
          id:data.id
        },
      });
    }else{
      const res = await this.app.model.TProject.create(data, {
        where: {},
      });
    }
  
    this.ctx.body = {
      code: 200,
    };
  }
  async saveDataSource() {
    const data = this.ctx.body;
    const res = await this.app.model.TDataSource.update(data, {
      where: {},
    });
    return res;
  }
  async createDataSource() {
    const data = this.ctx.body;
    const res = await this.app.model.TDataSource.create(data);
    return res;
  }
  async createApi() {
    const data = this.ctx.request.body;
    const res = await this.app.model.TApi.create(data);
    this.ctx.body = res;
  }
  async saveApi() {
    const data = this.ctx.request.body;
    // data.sqls = JSON.stringify(data.sqls)
    const res = await this.app.model.TApi.update(data, {
      where: { id: data.id },
    });
    this.ctx.body = res;
  }
  async findApi() {
    const query = this.ctx.request.query
    this.ctx.body = await this.app.model.TApi.findOne({
      where: {
        id: query.id,
      },
    });
  }
  async queryApiGroup() {
    const body  = this.ctx.request.body
    const TApiGroup = this.app.model.TApiGroup;
    const TApi = this.app.model.TApi;
    async function getRecursiveMenus() {
      const results = [];
      const menus = await TApiGroup.findAll({
        where: { projectId:body.projectId },
        // attributes: [
        //   ["title",'label'],
         
        //   ["id",'key'],
        // ] // 只选择 field1 和 field2 字段

      });
      for (const menu of menus) {
        console.log('menumenu',menu)
        const children = await getApiItems(menu.id);
        results.push({ ...menu.toJSON(), children });
      }
      return results;
    }
    async function getApiItems  (groupId){
      const menus = await TApi.findAll({
        where: { groupId },
        // attributes: [
          // ["name",'label'],
          // ["id",'key'],
        // ] // 只选择 field1 和 field2 字段

      });
      return menus
    }
    const res = await getRecursiveMenus(0);
    this.ctx.body = res;
  }
  async saveApiGroup() {
    const data = this.ctx.request.body;
    if (data.id) {
      const res = await this.app.model.TApiGroup.update(data, {
        where: {
          id:data.id
        },
      });
    }else{
      console.log("datadata", data);
      const res = await this.app.model.TApiGroup.create(data);
    }
   
    this.ctx.body = {
      code: 200,
    };
  }
  async queryApi() {
    const body = this.ctx.request.body;
    const page = 1; // 当前页码
    const limit = 10; // 每页显示的记录数
    const offset = (page - 1) * limit; // 计算偏移量

    const { rows, count } = await this.app.model.TApi.findAndCountAll({
      where: {
        projectId: body.projectId,
      },
      limit,
      offset,
    });
    this.ctx.body = {
      data: rows,
      total: count,
      success: true,
    };
  }

  async index() {
    const res = await this.app.model.TDatasource.findOne({
      where: {
        id: 1,
      },
    });

    try {
      const ss = await this.app.Sequelize.clients[res.name].query(
        "SELECT * FROM t_datasource"
      );
      this.ctx.body = ss;
    } catch (error) {
      this.ctx.body = "连接错误";
    }
  }
}

module.exports = SiteController;
